/**

The Notice below must appear in each file of the Source Code of any
copy you distribute of the Licensed Product.  Contributors to any
Modifications may add their own copyright notices to identify their
own contributions.

License:

The contents of this file are subject to the CognitiveWeb Open Source
License Version 1.1 (the License).  You may not copy or use this file,
in either source code or executable form, except in compliance with
the License.  You may obtain a copy of the License from

  http://www.CognitiveWeb.org/legal/license/

Software distributed under the License is distributed on an AS IS
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
the License for the specific language governing rights and limitations
under the License.

Copyrights:

Portions created by or assigned to CognitiveWeb are Copyright
(c) 2003-2003 CognitiveWeb.  All Rights Reserved.  Contact
information for CognitiveWeb is available at

  http://www.CognitiveWeb.org

Portions Copyright (c) 2002-2003 Bryan Thompson.

Acknowledgements:

Special thanks to the developers of the Jabber Open Source License 1.0
(JOSL), from which this License was derived.  This License contains
terms that differ from JOSL.

Special thanks to the CognitiveWeb Open Source Contributors for their
suggestions and support of the Cognitive Web.

Modifications:

*/
/*
 * Created on Jun 17, 2008
 */

package com.bigdata.rdf.lexicon;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.openrdf.model.Literal;
import org.openrdf.model.impl.LiteralImpl;
import com.bigdata.rdf.internal.IV;
import com.bigdata.rdf.model.BigdataValue;
import com.bigdata.rdf.model.BigdataValueFactory;
import com.bigdata.rdf.store.AbstractTripleStore;
import com.bigdata.rdf.store.AbstractTripleStoreTestCase;

/**
 * Unit tests for
 * {@link LexiconRelation#prefixScan(org.openrdf.model.Literal[])}.
 * 
 * @author <a href="mailto:thompsonbry@users.sourceforge.net">Bryan Thompson</a>
 */
public class TestCompletionScan extends AbstractTripleStoreTestCase {

    /**
     * 
     */
    public TestCompletionScan() {
    }

    /**
     * @param name
     */
    public TestCompletionScan(String name) {
        super(name);
    }
    
    /**
     * Unit test for a completion scan.
     */
    public void test_completionScan() {
        
        final AbstractTripleStore store = getStore();

        try {

            /*
             * Populate the KB with some terms. 
             */
            {
                
                final BigdataValueFactory f = store.getValueFactory();
                
                final BigdataValue[] terms = new BigdataValue[] {
                        
                        f.createLiteral("mingus"),
                        f.createLiteral("minor"),
                        f.createLiteral("minor league"),
                        f.createLiteral("minor threat"),
                        f.createLiteral("minority report"),
                        f.createLiteral("missing report"),
                        
                };
                
                store.addTerms(terms);
                
            }

            /*
             * Do a completion scan on "minor".
             */
            {
                
                final Set<Literal> expected = new HashSet<Literal>();
                
                expected.add(new LiteralImpl("minor"));

                expected.add(new LiteralImpl("minor league"));
                
                expected.add(new LiteralImpl("minor threat"));

                expected.add(new LiteralImpl("minority report"));
                
                final Iterator<IV> itr = store.getLexiconRelation()
                        .prefixScan(new LiteralImpl("minor"));

                while(itr.hasNext()) {
                    
                    if(expected.isEmpty()) {
                        
                        fail("Nothing else is expected");
                        
                    }
                    
                    final IV tid = itr.next();
                    
                    final Literal lit = (Literal) store.getLexiconRelation()
                            .getTerm(tid);
                    
                    if (log.isInfoEnabled())
                        log.info("Found: " + lit);
                    
                    if(!expected.remove(lit)) {
                        
                        fail("Not expecting: "+lit);
                        
                    }
                    
                }

                if(!expected.isEmpty()) {
                    
                    fail("Additional terms were expected: not found="+expected);
                    
                }
                
            }
            
        } finally {
            
            store.__tearDownUnitTest();
            
        }
        
    }

}
